include config

all:	udmd udm-stat udm-cmd udm-2388 udm-benc udm-down udm-trans
	@echo Preparing udm.conf...
	sed "s|\$$CTORDIR|${CTORDIR}|g" udm.conf.template > udm.conf
	sed -i "s|\$$WGETDIR|${WGETDIR}|g" udm.conf
	sed -i "s|\$$MTSTART|${MTSTART}|g" udm.conf
	sed -i "s|\$$MTSTOP|${MTSTOP}|g" udm.conf
	sed -i "s|\$$PIDFILE|${PIDFILE}|g" udm.conf
	sed -i "s|\$$DOWNDIR|${DOWNDIR}|g" udm.conf
	sed -i "s|\$$DWWW|${DWWW}|g" udm.conf
	sed -i "s|\$$DFB|${DFB}|g" udm.conf
	sed -i "s|\$$ADDPATH|${ADDPATH}|g" udm.conf
	sed -i "s|\$$VERSION|${VERSION}|g" udm.conf
	sed -i "s|\$$SOFTWARE|${SOFTWARE}|g" udm.conf
	sed -i "s|\$$AUTHOR|${AUTHOR}|g" udm.conf
	sed -i "s|\$$URL|${URL}|g" udm.conf
	sed -i "s|\$$EMAIL|${EMAIL}|g" udm.conf
	@echo Preparing readme...
	sed "s|\$$VERSION|${VERSION}|g" readme.template > readme
	sed -i "s|\$$SOFTWARE|${SOFTWARE}|g" readme
	sed -i "s|\$$EMAIL|${EMAIL}|g" readme
	sed -i "s|\$$YEAR|${YEAR}|g" readme
	sed -i "s|\$$URL|${URL}|g" readme
	sed -i "s|\$$SOFTWARE|${SOFTWARE}|g" readme
	sed -i "s|var version.*|var version=\'${VERSION}\';|g" udm.js
	@echo Preparing mini_httpd.udm
	sed "s|\$$USER|${USER}|g" mini_httpd.udm.template > mini_httpd.udm
	sed -i "s|\$$CPORT|${CPORT}|g" mini_httpd.udm
	sed -i "s|\$$DWWW|${DWWW}|g" mini_httpd.udm
	@echo Preparing start.sh
	sed "s|\$$CPORT|${CPORT}|g" start.sh.template > start.sh
	sed -i "s|\$$SOFTWARE|${SOFTWARE}|g" start.sh
	@echo Preparing stop.sh
	sed "s|\$$SOFTWARE|${SOFTWARE}|g" stop.sh.template > stop.sh
	@echo Preparing index.html
	sed -i "s|<VERSION>.*</VERSION>|<VERSION>${VERSION}</VERSION>|g" index.html
	sed -i "s|<YEAR>.*</YEAR>|<YEAR>${YEAR}</YEAR>|g" index.html
	@echo Preparing version.html
	echo ${MAJOR} > version.html
	@echo Preparing _welcome.nfo
	sed "s|\$$VERSION|${VERSION}|g" _welcome.nfo.template > _welcome.nfo
	sed -i "s|\$$SOFTWARE|${SOFTWARE}|g" _welcome.nfo
	cd extra; ./extra.sh

udmd:	udmd.o udm-common.o udm-fork.o udm-proc.o
	${CC} ${CFLAGS} ${LDFLAGS} udmd.o udm-common.o udm-proc.o udm-fork.o ${LDLIBS} -o udmd; ${STRIP} udmd

udm-stat:	udm-status.o udm-getpost.o udm-proc.o udm-common.o udm-response.o
	${CC} ${CFLAGS} ${LDFLAGS} udm-status.o udm-getpost.o udm-proc.o udm-common.o udm-response.o ${LDLIBS} -o udm-stat.cgi; ${STRIP} udm-stat.cgi

udm-benc:	udm-bencode.o udm-getpost.o udm-common.o udm-response.o
	${CC} ${CFLAGS} ${LDFLAGS} udm-bencode.o udm-getpost.o udm-common.o udm-response.o ${LDLIBS} -o udm-benc.cgi; ${STRIP} udm-benc.cgi

udm-cmd:	udm-command.o udm-getpost.o udm-proc.o udm-common.o udm-fork.o udm-response.o
	${CC} ${CFLAGS} ${LDFLAGS} udm-command.o udm-getpost.o udm-proc.o udm-common.o udm-fork.o udm-response.o ${LDLIBS} -o udm-cmd.cgi; ${STRIP} udm-cmd.cgi

udm-down:	udm-download.o udm-getpost.o udm-common.o udm-response.o
	${CC} ${CFLAGS} ${LDFLAGS} udm-download.o udm-getpost.o udm-common.o udm-response.o ${LDLIBS} -o udm-down.cgi; ${STRIP} udm-down.cgi

udm-2388:	udm-rfc2388.o udm-common.o udm-response.o
	${CC} ${CFLAGS} ${LDFLAGS} udm-rfc2388.o udm-common.o udm-response.o ${LDLIBS} -o udm-2388.cgi; ${STRIP} udm-2388.cgi

udm-trans:	udm-trans.o udm-getpost.o udm-proc.o udm-common.o udm-response.o
	${CC} ${CFLAGS} ${LDFLAGS} udm-trans.o udm-getpost.o udm-proc.o udm-common.o udm-response.o ${LDLIBS} -o udm-trans.cgi; ${STRIP} udm-trans.cgi

.o:	udm-common.h udm-version.h
	$(CC) $(CFLAGS) -c $*.c

install:
	echo Installing udm...
	-rm -r ${BINDIR}/udm-${VERSION}
	-mkdir -p ${BINDIR}/udm-${VERSION}
	-mkdir -p ${BINDIR}/udm-${VERSION}/bin
	-mkdir -p ${BINDIR}/udm-${VERSION}/scripts
	-mkdir -p ${BINDIR}/udm-${VERSION}/download
	cp udm.conf readme mini_httpd.udm start.sh stop.sh ${BINDIR}/udm-${VERSION}
	cp udmd ${BINDIR}/udm-${VERSION}/bin
	cp extra/bin/* ${BINDIR}/udm-${VERSION}/bin
	-mkdir -p ${BINDIR}/udm-${VERSION}/www
	cp -r www ${BINDIR}/udm-${VERSION}/www/udm
	-mkdir -p ${BINDIR}/udm-${VERSION}/www/udm/cgi-bin
	-mkdir -p ${BINDIR}/udm-${VERSION}/www/udm/trk
	cp index.html ${BINDIR}/udm-${VERSION}/www/udm
	cp index_root.html ${BINDIR}/udm-${VERSION}/www/index.html
	cp readme ${BINDIR}/udm-${VERSION}/www/udm/readme.txt
	cp version.html ${BINDIR}/udm-${VERSION}/www/udm
	cp udm.js ${BINDIR}/udm-${VERSION}/www/udm/js/udm.js
	cp udm.css ${BINDIR}/udm-${VERSION}/www/udm/css
	cp _welcome.nfo ${BINDIR}/udm-${VERSION}/www/udm/trk
	echo "Initializing..." > ${BINDIR}/udm-${VERSION}/www/udm/trk/amule.amu
	cp udm-stat.cgi udm-benc.cgi udm-cmd.cgi udm-down.cgi udm-trans.cgi udm-2388.cgi ${BINDIR}/udm-${VERSION}/www/udm/cgi-bin
	echo "#!/bin/sh" > ${BINDIR}/udm-${VERSION}/scripts/helloworld.sh
	echo "echo Hello World!" >> ${BINDIR}/udm-${VERSION}/scripts/helloworld.sh
	chmod -R a-x ${BINDIR}/udm-${VERSION}/
	chmod a+x ${BINDIR}/udm-${VERSION}/bin/*
	chmod a+x ${BINDIR}/udm-${VERSION}/scripts/*
	chmod a+x ${BINDIR}/udm-${VERSION}/www/udm/cgi-bin/*
	chmod a+x ${BINDIR}/udm-${VERSION}/start.sh
	chmod a+x ${BINDIR}/udm-${VERSION}/stop.sh
	-chmod a+wr ${LOGFILE}
	touch -t 01010101 ${BINDIR}/udm-${VERSION}/www/udm/version.html
	echo preparing file browser...
	-mkdir -p ${BINDIR}/udm-${VERSION}/www/${DFB}
	-mkdir -p ${BINDIR}/udm-${VERSION}/www/${DFB}/cgi-bin
	cp -r www/* ${BINDIR}/udm-${VERSION}/www/${DFB}
	cp index_udmfb.html ${BINDIR}/udm-${VERSION}/www/${DFB}/index.html
	cp udm.js ${BINDIR}/udm-${VERSION}/www/${DFB}/js/udm.js
	cp udm.css ${BINDIR}/udm-${VERSION}/www/${DFB}/css
	cp udm-stat.cgi udm-down.cgi ${BINDIR}/udm-${VERSION}/www/${DFB}/cgi-bin
	chmod a+x ${BINDIR}/udm-${VERSION}/www/${DFB}/cgi-bin/*
	echo done.

package:
	cd ${BINDIR}; tar cvfz udm-${VERSION}.tar.gz udm-${VERSION}; rm -rf udm-${VERSION}

clean:
	-rm -f *.o *.cgi udmd udm.conf start.sh stop.sh version.html mini_httpd.udm _welcome.nfo
	-rm -rf extra/bin extra/working
	-rm -rf ${BINDIR}/udm-${VERSION}

dist:
	-mkdir udm-${VERSION}-src
	tar cf - `cat FILES` | ( cd udm-${VERSION}-src ; tar xf - )
	tar cvfz udm-${VERSION}-src.tar.gz udm-${VERSION}-src
	rm -r udm-${VERSION}-src

start:
	cd ${BINDIR}/udm-${VERSION}; ./start.sh

stop:
	cd ${BINDIR}/udm-${VERSION}; ./stop.sh
